from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
from django.http import HttpResponse, Http404
from django.contrib.auth.models import User
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.contrib.auth import logout
from django.shortcuts import render_to_response
from forfood.restaurant.models import *
from forfood.menu.models import *
from forfood.helpers.user_helper import *
from forfood.order.models import *
from forfood.menu.models import MenuItem
from time import time

def create_order(request):
  #  if 'menu_item_num' in request.GET and 'menu_item_id' not in request.GET:
  #      return HttpResponse('The item does not exist!')
  #  elif 'menu_item_num' not in request.GET and  'menu_item_id' in request.GET:
  #      return HttpResponse('The menu item num does not exist!')
  #  elif 'menu_item_num' not in request.GET and 'menu_item_id' not in request.GET:
  #      return HttpResponse('The menu item and menu item num do not exist!')
  #  else:
  #      return HttpResponse('The item does not exist!')
    if request.method == 'POST':
        customer = find_customer(request.user)
        if not customer:
            raise Http404
      
        restaurant = find_restaurant_by_id(request.POST['restaurant_id'])
        if not restaurant:
            raise Http404
      
        d = request.POST
        if len(d.keys()) <= 2:
            print "xxx"
            raise Http404
        order = Order.objects.create(customer=customer, restaurant=restaurant)
        count = 0
        total = 0
        for k in d.keys():
            try:
                k_int = int(k)
            except:
                continue
      
            menu_item = MenuItem.objects.get(id=k_int)
            order_item = OrderItem.objects.create(order=order, item=menu_item, count=d[k])   
            count = count + 1
            total = total + menu_item.price
            #order.orderitem_set.add(order_item) 
        #order.save()
      
        return HttpResponseRedirect('/order/%d/'%order.id)
    else:
        raise Http404
    
def orders_page(request):
    customer = find_customer(request.user)
    if not customer:
        raise Http404
    
    return render_to_response('order/all.html', RequestContext(request,locals()))   
     
def order_page(request, oid):
    try:
        order = Order.objects.get(id = oid)
    except:
        raise Http404
    
    customer = find_customer(request.user)
    if not customer:
        raise Http404
    
    if order.customer != customer:
        raise Http404
       
    if request.method == 'POST':
        order.status = 1
        order.c_confirm_time = int(time())
        order.save()
        pass
    
    return render_to_response('order/main.html', RequestContext(request,locals()))

def change_order_status(request, oid):
    try:
        order = Order.objects.get(id = oid)
    except:
        raise Http404
    
    restaurant = find_restaurant(request.user)
    if order.restaurant != restaurant:
        raise Http404
    
    if order.status > 3:
        raise Http404
    
    order.status = order.status + 1
    order.save()
    order.set_time_by_status()
    return HttpResponse("success")

def undo_change_order_status(request, oid):
    try:
        order = Order.objects.get(id = oid)
    except:
        raise Http404
    
    restaurant = find_restaurant(request.user)
    if order.restaurant != restaurant:
        raise Http404
    
    if order.status < 2:
        raise Http404
    
    order.status = order.status - 1
    order.save()
    return HttpResponse("success")